//
//  DYJingquJianjieViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import SDCycleScrollView
import YYText

class DYJingquJianjieViewController: SYBaseViewController {

    
    var id: String!
    public var bgView: UIScrollView = {
        let v:UIScrollView = UIScrollView()
        
        return v
    }()
    
    let textView: UILabel = {
        let v: UILabel = UILabel()
        
        v.font = 13.autoFontSize()
        v.textColor = UIColor(hex: 0x666666)
        v.numberOfLines = 0

        return v
    }()
    
    let nameLb: YYLabel = {
        let v:YYLabel = YYLabel()
        
        return v
    }()
    
    let cycleScrollView: SDCycleScrollView = SDCycleScrollView(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 220.0.autoWidth()))

    init(model: DYTourScenicVisitsModel) {
        super.init(nibName: nil, bundle: nil)
        self.id = model.id
        
        self.getData()
    }
    
    init(m: DYJQYoulanListModel) {
        super.init(nibName: nil, bundle: nil)
        
        self.id = m.id
        self.getData()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
    }
    
    func getData() {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_jingquYoulanDetail(id: self.id), modelClass: SGBaseModel<DYJQYoulanDetail>.self) {[weak self] (res) in
            if let `self` = self {
                self.loadSuccess(res.data)
            }
            
        } Error: {
            
        }
    }
    
    func loadSuccess(_ model: DYJQYoulanDetail?) {
        
        self.title = model?.name
        cycleScrollView.imageURLStringsGroup = ["\(IMGIP + (model?.imrUrl ?? ""))"]
        
        //通过富文本来设置行间距
        let  paraph =  NSMutableParagraphStyle ()
        //将行间距设置为28
        paraph.lineSpacing = 10
        //样式属性集合
        let  attributes = [ NSAttributedString.Key.font : UIFont .systemFont(ofSize: 13),NSAttributedString.Key.paragraphStyle : paraph]
        textView.attributedText =  NSAttributedString (string: model?.synopsis ?? "", attributes: attributes)
        
        
        let firStr = "位于\(model?.city ?? "")"
        let totalStr = "\(firStr) \(model?.address ?? "")"
        let attString = NSMutableAttributedString(string: totalStr)
        attString.yy_setColor(UIColor(hex: 0x1E1E1E), range: NSRange(location: 0, length: firStr.count))
        attString.yy_setFont(14.autoFontSize(), range: NSRange(location: 0, length:firStr.count))
        attString.yy_setColor(UIColor(hex: 0x3C72F4), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setFont(16.autoFontSize(), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
//        attString.yy_setAlignment(.center, range: NSRange(location: 0, length: totalStr.count))
        nameLb.attributedText = attString
        
        contentView()
    }

    
    func cycleView() {
        
        
//        cycleScrollView.placeholderImage = UIImage(named: "banner")
        // 添加到view
        bgView.addSubview(cycleScrollView)
    }
    
    func contentView() {
        
        let titleLb: UILabel = {
            let v:UILabel = UILabel()
            v.textColor = UIColor(hex: 0x1E1E1E)
            v.font = 18.autoFontSize()
            v.text = "景点信息"
            return v
        }()
        
        
        
        let locationLb: UILabel = {
            let v:UILabel = UILabel()
            v.textColor = UIColor(hex: 0x1E1E1E)
            v.font = 14.autoFontSize()
            v.text = "简介"
            return v
        }()
        
        let pic: UIImageView = {
            let v: UIImageView = UIImageView()
            v.image = UIImage(named: "jianjie")
            return v
        }()
        
        let locationPic: UIImageView = {
            let v: UIImageView = UIImageView()
            v.image = UIImage(named: "location_daolan")
            return v
        }()
        
        

        
        func initViews() {
            
            //定义一个constrainSize值用于计算textview的高度
            let constrainSize = CGSize(width:SCREEN_WIDTH - 70.autoWidth(),height:CGFloat(MAXFLOAT))
            
            //获取textview的真实高度
            let size = textView.sizeThatFits(constrainSize)
            if size.height > SCREEN_HEIGHT - 319.0.autoWidth() - NAV_HEIGHT - BOTTOM_SAFE_HEIGHT {
                bgView.contentSize = CGSize(width: SCREEN_WIDTH, height: size.height + 319.0.autoWidth())
            } else {
                bgView.contentSize = CGSize(width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 319.0.autoWidth() - NAV_HEIGHT - BOTTOM_SAFE_HEIGHT)
            }
            bgView.frame = CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - NAV_HEIGHT - BOTTOM_SAFE_HEIGHT)
            
            bgView.showsVerticalScrollIndicator = false
            bgView.showsHorizontalScrollIndicator = false
            self.view.addSubview(bgView)
            cycleView()
            
            
            
            bgView.addSubview(titleLb)
            bgView.addSubview(nameLb)
            bgView.addSubview(locationLb)
            bgView.addSubview(pic)
            bgView.addSubview(locationPic)
            bgView.addSubview(textView)
            titleLb.snp.makeConstraints { (make) in
                make.top.equalTo(cycleScrollView.snp.bottom).offset(15.0.autoWidth())
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(15.5.autoWidth())
            }
            nameLb.snp.makeConstraints { (make) in
                make.centerY.equalTo(pic)
                make.left.equalTo(pic.snp.right).offset(autoWidth(6.5))
                make.right.equalTo(self.view).offset(autoWidth(-15.0))
                make.height.equalTo(15.5.autoWidth())
            }
            locationPic.snp.makeConstraints { (make) in
                make.top.equalTo(pic.snp.bottom).offset(15.0.autoWidth())
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(13.0.autoWidth())
                make.width.equalTo(11.0.autoWidth())
            }
            pic.snp.makeConstraints { (make) in
                make.top.equalTo(titleLb.snp.bottom).offset(15.0.autoWidth())
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(13.5.autoWidth())
                make.width.equalTo(11.5.autoWidth())
            }
            locationLb.snp.makeConstraints { (make) in
                make.centerY.equalTo(locationPic)
                make.left.equalTo(locationPic.snp.right).offset(autoWidth(6.5))
                make.right.equalTo(self.view).offset(autoWidth(-15.0))
                make.height.equalTo(15.5.autoWidth())
            }
            textView.snp.makeConstraints { (make) in
                make.top.equalTo(locationPic.snp.bottom).offset(10.0.autoWidth())
                make.left.equalToSuperview().offset(autoWidth(35.0))
                make.right.equalTo(self.view).offset(autoWidth(-35.0))
            }
        }
        initViews()
    }
  
}
